﻿using ImTools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection.Emit;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TangGuo.Resources;

namespace TangGuo.UserControls
{
    /// <summary>
    /// Interaction logic for TangGuoBox
    /// </summary>
    public partial class TangGuoBox : UserControl
    {
        public int Col;
        public int Row;
        int PieceLevel = 0;
        bool onMoveCompletSetDad = false;
        public int GetNumber() => PieceLevel;
        public bool IsBlank() => PieceLevel == 0;

        public List<Storyboard> storyboardList;
        public ArrayList posList;
        public TangGuoBox()
        {
            InitializeComponent();
            posList = new();
            storyboardList = new();
        }
        public TangGuoBox(int x, int y)
        {
            InitializeComponent();
            SetPos(x, y);
            posList = new();
            storyboardList = new();
        }
        public TangGuoBox(int x, int y, int number)
        {
            InitializeComponent();
            SetPos(x, y);
            GameStartSetNumber(number);

            posList = new();
            storyboardList = new();
        }
       
        public bool IsDad() => onMoveCompletSetDad;
        public void SetPos(int x, int y)
        {
            Col = x;
            Row = y;
        }
        public void SetPos(BasePos pos)
        {
            if (pos != null) SetPos(pos.Col, pos.Row);
        }
        public void SetLocation()
        {
            int x = Col;
            int y = Row;
            double xd = (y % 2) == 0 ? x * 90.0 : 45.0 + x * 90.0;
            double yd = y * 77;
            SetValue(Canvas.LeftProperty, xd);
            SetValue(Canvas.TopProperty, yd);
        }
        public static double GetXByPos(BasePos pos)
        {
            int x = pos.Col; int y = pos.Row;
            double xd = (y % 2) == 0 ? x * 90.0 : 45.0 + x * 90.0;
            return xd;
        }

        public static double GetYByPos(BasePos pos)
        {
            if (pos != null)
            {
                int y = pos.Row;
                double yd = y * 77;
                return yd;
            }
            return 0;
        }
        public BasePos GetBasePos()
        {
            return new BasePos(Col, Row);
        }
        public void GameStartSetNumber(int level)
        {
            PieceLevel = level;
            if (PieceLevel == 0)
            {
                TangGuoGrid.SetValue(OpacityProperty, 0.0);
            }
            else
            {
                TangGuoGrid.SetValue(OpacityProperty, 1.0);
                TangGuoGrid.Background = this.FindResource($"TangGuo_{PieceLevel}") as ImageBrush;
            }
        }
        
        public void SetDad()
        {
            onMoveCompletSetDad=true;
            Visibility = Visibility.Hidden;
            IsHitTestVisible = false;
            SetPos(1, 6);

        }
        public void SetDadOnMoveCompleted()
        {
            onMoveCompletSetDad = true;
        }
        /// <summary>
        /// 修改PieceLevel的值
        /// </summary>
        /// <param name="level"></param>
        public void SetNumber(int level)
        {
            if (level is >= 1 and <= 35)
            {
                PieceLevel = level;
                DoubleAnimation doubleAnimationIn = new() { From = 0.0, To = 1.0, Duration = TimeSpan.FromMilliseconds(50) };
                Storyboard.SetTargetProperty(doubleAnimationIn, new PropertyPath(OpacityProperty));

                Storyboard.SetTargetName(doubleAnimationIn, this.Name);
                Storyboard storyboard = new() { FillBehavior = FillBehavior.Stop, AutoReverse = false };
                storyboard.Completed += Storyboard_Completed;
                storyboard.Children.Add(doubleAnimationIn);
                storyboardList.Add(storyboard);
                

                Visibility = Visibility.Visible;
            }
            else
            {
                Visibility = Visibility.Hidden;
            }
            if (Row % 2 == 1 && Col == 6)
            {
                Visibility = Visibility.Hidden;
                IsHitTestVisible = false;
                PieceLevel = 10; // 隐藏的障碍物
            }
        }


        /// <summary>
        /// 糖果动画结束时，按其最终属性设置其状态。因为在动画执行过程中，PieceLevel很大可能已经出现了变化，例如，在初始化的时候。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Storyboard_Completed(object sender, EventArgs e)
        {
            if (PieceLevel == 0)
            {
                SetValue(OpacityProperty, 0.0);
            }
            else
            {
                TangGuoGrid.Background = this.FindResource($"TangGuo_{PieceLevel}") as ImageBrush;
                SetValue(OpacityProperty, 1.0);
                Opacity = 1.0;
                
                SetLocation();
            }
            if (onMoveCompletSetDad==true) { Visibility = Visibility.Hidden; Opacity = 0.0; }
        }
        /// <summary>
        /// 鼠标键在网格上按下时的处理程序，当鼠标键松开时，事件冒泡到主程序处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TangGuo_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            PublicData.IsTangGuoSelect = true;
            PublicData.TangGuoSelectedCol = Col;
            PublicData.TangGuoSelectedRow = Row;
            PublicData.IsTangGuoBlankSelect = false;
            PublicData.LastSelectIsNumber = true;
        }

        
    }
}
